package com.example.boxuegu.DengZhu;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxuegu.MD5;
import com.example.boxuegu.R;
import com.example.boxuegu.find_psw;

public class MyDenglu extends AppCompatActivity {

    private ImageView fanhui;
    private EditText dl_user;
    private EditText dl_psw;
    private TextView dl_zc;
    private TextView dl_wj;
    private Button dl_dl;
    private String username,paw,sppaw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_denglu);
        init();
    }

    private void init() {
        fanhui = findViewById(R.id.fanhui);//返回图标
        dl_user = findViewById(R.id.dl_user);//user输入框
        dl_psw = findViewById(R.id.dl_psw);//密码输入框
        //注册文本
        dl_zc = findViewById(R.id.dl_zc);
        //忘记密码文本
        dl_wj = findViewById(R.id.dl_wj);
        //登录按钮
        dl_dl = findViewById(R.id.dl_dl);

        /*
         * 返回按钮点击事件*/
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDenglu.this.finish();
            }
        });

        //注册控件点击事件
        dl_zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyDenglu.this, Myzhuce.class);
                startActivityForResult(intent,1);
            }
        });

        //忘记密码控件
        dl_wj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyDenglu.this, find_psw.class);
                startActivity(intent);
            }
        });
        /*
        * 登陆点击事件*/
        dl_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = dl_user.getText().toString().trim();
                paw = dl_psw.getText().toString().trim();
                String md5 = MD5.md5(paw);
                sppaw = readpsw(username);
                if(TextUtils.isEmpty(username)){
                    Toast.makeText(MyDenglu.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(paw)){
                    Toast.makeText(MyDenglu.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(md5.equals(sppaw)){
                    Toast.makeText(MyDenglu.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    //保存登录状态和登陆的用户名
                    save(true,username);
                    //把登陆成功的状态传递到MainActivity中
                    Intent data=new Intent();
                    data.putExtra("isLogin",true);
                    setResult(RESULT_OK,data);
                    MyDenglu.this.finish();
                    return;
                }else if(!TextUtils.isEmpty(sppaw)&&!md5.equals(sppaw)){
                    Toast.makeText(MyDenglu.this,"输入用户名和密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(MyDenglu.this,"此用户名不存在",Toast.LENGTH_SHORT).show();
                }
            }
        });


        //登录按钮的点击事件

    }
    private String readpsw(String username) {
        SharedPreferences loginINfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return loginINfo.getString(username,"");
    }
    private void save(boolean b, String username) {
    //loginInfo表示文件名
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=loginInfo.edit();//获取编译器
        editor.putBoolean("isLogin",b);
        editor.putString("logusername",username);
        editor.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            String username=data.getStringExtra("username");
            if(!TextUtils.isEmpty(username)){
                dl_user.setText(username);
                dl_user.setSelection(username.length());
            }

        }
    }
}